from apps.common.base.views import *
from apps.pay.order.dao import OrderDao
from apps.merchant.item.dao import ItemDao
from django.db.models import Sum
from decimal import Decimal
from apps.system.lessee.dao import LesseeDao
from apps.user.dao import UserDao
from apps.pay.recordIncomes.dao import RecordIncomesDao

class LargeDataViewSet(BaseViewSet):
    '''总体数据'''

    @action(methods=['post'], detail=False)
    def getTotalityData(self, request):
        params = getRequestParams(request)
        data = {}
        _lessee = LesseeDao().getLessee()
        data['shopName'] = _lessee.name
        data['freePoolDay'] = str(_lessee.freePoolDay)
        data['freePoolMonth'] = str(_lessee.freePoolMonth)
        data['operationAwardPool'] = str(_lessee.operationAwardPool)
        data['supplyAwards'] = str(_lessee.supplyAwards)
        data['helpFundPool'] = str(_lessee.helpFundPool)
        data['grossProfit'] = str(_lessee.grossProfit)
        # 订单
        orders = OrderDao().findAllSubOrder()
        data['wait_send_count'] = orders.filter(orderStatus='WAIT_SEND').count()
        data['wait_rake_count'] = orders.filter(orderStatus='WAIT_RECEIVE').count()
        # 商品
        items = ItemDao().findAll()
        data['selling_count'] = items.filter(checkStatus='PASS', status=1).count()
        data['stock_short_count'] = 0
        # 实时数据
        #总购物积分
        users = UserDao().findAllCustom()
        incomesAll = users.aggregate(sums=Sum('incomes'))['sums'] or 0
        data['incomesAll'] = str(incomesAll)
        #总锁定补贴
        incomesLocked = RecordIncomesDao().findAll().filter(payway='incomesLocked', changeWay='add', moneyLocked__gt=0).aggregate(sums=Sum('moneyLocked'))['sums'] or 0
        data['incomesLocked'] = str(incomesLocked)
        #总贡献值
        contributionValue = RecordIncomesDao().findAll().filter(payway='contributionValue', changeWay='add', money__gt=0).aggregate(sums=Sum('money'))['sums'] or 0
        data['contributionValue'] = str(contributionValue)
        #抵扣积分
        incomesCreditAll = users.aggregate(sums=Sum('incomesCredit'))['sums'] or 0
        data['incomesCreditAll'] = str(incomesCreditAll)
        #原积分
        incomesOriginalAll = users.aggregate(sums=Sum('incomesOriginal'))['sums'] or 0
        data['incomesOriginalAll'] = str(incomesOriginalAll)
        # 提现中
        outMoneying = RecordIncomesDao().findAll().filter(payway='incomes', scene='outMoney', orderStatus='WAIT_SEND').aggregate(sums=Sum('money'))['sums'] or 0
        data['outMoneying'] = str(outMoneying)

        stock_short_items = ItemDao().findAll().filter(item_sku__stock__lt=10)
        data['stock_short_count'] = stock_short_items.count()
        return Response(response_success(data))
